<template>
  <div class="pie-container">
    <div ref="pieChart" class="pie-chart"></div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import * as echarts from 'echarts'
import axios from 'axios'

const props = defineProps({
  province: {
    type: String,
    default: ''
  }
})

const pieChart = ref(null)
let chart = null
const predictData = ref({})

// 获取预测数据
const fetchPredictData = async () => {
  try {
    const response = await axios.get('http://localhost:8081/tcmsampledata/predict')
    if (response.data.code === 200) {
      predictData.value = response.data.data
      updateChart()
    }
  } catch (error) {
    console.error('获取预测数据失败:', error)
  }
}

// 更新图表
const updateChart = () => {
  if (!props.province || !predictData.value[props.province]) return

  const provinceData = predictData.value[props.province]
  const physiqueCount = {}

  // 合并男女数据
  Object.values(provinceData).forEach(genderData => {
    Object.entries(genderData).forEach(([physique, count]) => {
      physiqueCount[physique] = (physiqueCount[physique] || 0) + count
    })
  })

  // 体质颜色映射
  const physiqueColors = {
    '平和质': '#67C23A',
    '气虚质': '#E6A23C',
    '阳虚质': '#F56C6C',
    '阴虚质': '#409EFF',
    '痰湿质': '#909399',
    '湿热质': '#9C27B0',
    '血瘀质': '#F44336',
    '气郁质': '#FF9800',
    '特禀质': '#795548'
  }

  const seriesData = Object.entries(physiqueCount).map(([physique, count]) => ({
    name: physique,
    value: count,
    itemStyle: {
      color: physiqueColors[physique] || '#909399'
    }
  }))

  const option = {
    title: {
      text: `KNN算法预测${props.province}未来体质趋势`,
      left: 'center',
      textStyle: {
        color: '#32ccee',  
        fontSize: 14
      }
    },
    tooltip: {
      trigger: 'item',
      formatter: '{b}: {c} ({d}%)'
    },
    legend: {
      orient: 'vertical',
      left: 'left',
      textStyle: {
        color: '#fff',
        fontSize: 12
      }
    },
    series: [
      {
        name: '体质分布',
        type: 'pie',
        radius: ['30%', '60%'],
        center: ['50%', '50%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 8,
          borderColor: '#001432',
          borderWidth: 2
        },
        label: {
          show: true,
          color: '#fff',
          fontSize: 12,
          formatter: '{b}\n{c} ({d}%)'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: '14',
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: true,
          length: 10,
          length2: 10
        },
        data: seriesData
      }
    ]
  }

  if (chart) {
    chart.setOption(option)
  }
}

// 监听省份变化
watch(() => props.province, () => {
  updateChart()
})

onMounted(() => {
  chart = echarts.init(pieChart.value)
  fetchPredictData()
  window.addEventListener('resize', () => {
    chart?.resize()
  })
})
</script>

<style scoped>
.pie-container {
  width: 100%;
  height: 250px;
  background: rgba(0, 20, 50, 0.5);
  border-radius: 6px;
  padding: 8px;
}

.pie-chart {
  width: 100%;
  height: 100%;
}
</style> 